<template>
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/Home' }">商品管理</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/Home' }">商品分类</el-breadcrumb-item>
  </el-breadcrumb>

  <!-- 卡片视图 -->
  <el-card>
    <el-row>
      <el-col>
        <el-button type="primary" @click="openAddClassDialog"
          >添加分类</el-button
        >
      </el-col>

      <!-- 表格区域 -->
      <!-- <tree-table :data="cateList" :columns="columns">

    </tree-table> -->
      <el-table
        :data="cateList"
        style="width: 100%;margin-bottom: 20px;"
        row-key="cat_id"
        border
        :default-expand-all="false"
        :tree-props="{ children: 'children' }"
        class="treeTable"
      >
        <el-table-column prop="cat_name" label="分类名称" sortable width="180">
        </el-table-column>
        <el-table-column label="是否有效" sortable width="180">
          <template #default="scope">
            <i class="el-icon-check" v-if="!scope.row.deleted"></i>
            <i class="el-icon-close" v-else></i>
          </template>
        </el-table-column>
        <el-table-column label="排序">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.cat_level == 0">一级</el-tag>
            <el-tag type="warning" v-if="scope.row.cat_level == 1">二级</el-tag>
            <el-tag type="danger" v-if="scope.row.cat_level == 2">三级</el-tag>
            <!-- <el-tag type="info" v-else>四级</el-tag> -->
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <!-- 编辑操作 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top"
              :enterable="false"
            >
              <el-button type="primary" icon="el-icon-edit" size="mini"
                >编辑</el-button
              >
            </el-tooltip>

            <!-- 删除操作 -->

            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
              :enterable="false"
            >
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteCate(scope.row)"
                >删除</el-button
              >
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
  </el-card>

  <!-- 添加分类对话框 -->
  <el-dialog
    title="提示"
    v-model="addClassdialogVisible"
    width="40%"
    @close="addDialogClosed"
  >
    <!-- 表单区域 -->
    <el-form
      :model="addClassRuleForm"
      :rules="addClassRules"
      ref="addClassruleForm"
      label-width="100px"
      class="addClassForm"
    >
      <el-form-item label="分类名称" prop="cat_name">
        <el-input v-model="addClassRuleForm.cat_name"></el-input>
      </el-form-item>
      <el-form-item label="父级分类">
        <el-cascader
          v-model="cascaderValue"
          :options="parentCateList"
          :props="cascaderProps"
          placeholder="请选择"
          @change="parentCateChanged"
          :clearable="true"
        ></el-cascader>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addClassdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { getCateList, addCatePost,deleteCate } from "@/network/Goods.js";
export default {
  name: "categories",
  data() {
    return {
      cateList: [], //商品分类数据列表
      cateListProps: {
        children: "children",
        label: "cat_name",
      },
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },

      total: 0,
      addClassdialogVisible: false, //添加分类表单的显示
      //树形结构的定义
      // columns: [{ label: "分类名称", prop: "cat_name" }],

      // 添加分类的表单数据及验证规则
      addClassRuleForm: {
        cat_name: null, //分类名称
        cat_pid: 0, //父分类ID
        cat_level: 0, //分类等级
      }, //表单数据
      addClassRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      }, //验证规则
      parentCateList: [], //父级分类列表
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      }, //级联选择器的配置对象
      cascaderValue: [], //级联选择器的绑定值必须是一个数组，因为多级选择将同时确定多个数值
    };
  },
  methods: {
    async getCateList() {
      const { data: res } = await getCateList(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类失败");
      } else {
        // console.log(res.data.result);
        this.cateList = res.data.result;
        this.total = res.data.total;
      }
    },

    // 打开添加分类对话框
    openAddClassDialog() {
      this.getParentCateDialog();
      this.addClassdialogVisible = true;
    },

    //获取父级列表
    async getParentCateDialog() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });

      if (res.meta.status != 200) {
        this.$message.error("数据获取失败");
      } else {
        this.parentCateList = res.data;
        // console.log(this.parentCateList)
      }
    },

    // 选择项发生变化时触发的处理函数
    parentCateChanged() {
      // console.log(this.cascaderValue);
      //如果cascadervalue中的值不为空，证明已经选中了数值
      // console.log(this.cascaderValue[this.cascaderValue.length - 1]);
      if (this.cascaderValue.length > 0) {
        this.addClassRuleForm.cat_pid = this.cascaderValue[
          this.cascaderValue.length - 1
        ];
        this.addClassRuleForm.cat_level = this.cascaderValue.length;
        // console.log(this.addClassRuleForm)
      }
    },
    //点击按钮添加新的分类
    addCate() {
      this.$refs.addClassruleForm.validate(async (valid) => {
        if (!valid) {
          this.$message.error("输入不合法");
          return (this.addClassdialogVisible = false);
        } else {
          const { data: res } = await addCatePost(this.addClassRuleForm);
          if (res.meta.status != 201) {
            this.$message.error("添加分类失败");
          } else {
            this.$message.success("添加成功");
            this.addClassdialogVisible = false;
            console.log(res)
            this.getCateList();
          }
        }
      });
    },
    //监听对话框的关闭
    addDialogClosed() {
      this.$refs.addClassruleForm.resetFields();
      this.cascaderValue = [];
      this.cascaderProps.cat_pid = 0;
      this.cascaderProps.cat_level = 0;
    },
    //删除分类
    async deleteCate(cate){
       console.log(cate);
      const {data:res} = await deleteCate(cate.cat_id);
      if(res.meta.status != 200)
      {
        console.log(res);
        return this.$message.error("删除失败")
      }
      else{
         this.$message.success("删除成功")
         this.getCateList();
      }
    },

    // 分页区事件响应函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },
  },

  computed: {},
  created() {
    this.getCateList();
  },
};
</script>

<style lang="less" scoped>
.treeTable {
  box-sizing: border-box;
  margin-top: 10px;
}

.el-cascader {
  width: 100%;
}
</style>
